﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Aria2GUI
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            DownloadDirtextbox.Enter += new EventHandler(DownloadDirtextbox_Enter); //获得焦点事件
            //DownloadDirtextbox.Leave += new EventHandler(DownloadDirtextbox_Leave); //失去焦点事件。
        }

        private void NowDownloadbutton_Click(object sender, EventArgs e)
        {

            if (File.Exists(Application.StartupPath + "\\aria2c.exe"))
            {
                
            }
            else
            {
                MessageBox.Show("程序损坏！");
                return;
            }

            if (Downloadurltextbox.Text == "")
            {
                MessageBox.Show("请输入下载地址！");
                return;
            }

            if (Downloadurltextbox.Text.IndexOf("ed2k") > -1)
            {
                MessageBox.Show("暂不支持ed2k！");
                return;
            }

            if (Downloadurltextbox.Text.IndexOf("http") > -1 || Downloadurltextbox.Text.IndexOf("magnet") > -1)
            {

            }
            else
            {
                MessageBox.Show("下载地址格式有误！");
                return;
            }

            if (Downloadurltextbox.Text.IndexOf("magnet") > -1)
            {
                if (DownloadBTtextbox.Text == "")
                {
                    MessageBox.Show("请输入BT服务器地址！");
                    return;
                }
            }

            if (DownloadDirtextbox.Text == "")
            {
                MessageBox.Show("请选择保存目录！");
                return;
            }
            


            NowDownloadbutton.Text = "正在下载中";
            NowDownloadbutton.Enabled= false;
                //process.StandardInput.AutoFlush = true;
                //process.StandardInput.WriteLine("exit");
                //process.StandardInput.Close();


                //获取cmd窗口的输出信息
                Task newtask = new Task(() =>
                {
                    using (var process = new Process())
                    {
                        process.StartInfo.Arguments = "--bt-tracker="+DownloadBTtextbox.Text + " "+"--dir="+DownloadDirtextbox.Text+" "+Downloadurltextbox.Text;
                        process.StartInfo.FileName = "aria2c.exe";
                        process.StartInfo.UseShellExecute = false;
                        process.StartInfo.RedirectStandardInput = true;
                        process.StartInfo.RedirectStandardOutput = true;
                        process.StartInfo.RedirectStandardError = true;
                        process.StartInfo.CreateNoWindow = true;
                        process.Start();
                        process.StandardInput.AutoFlush = true;
                        process.StandardInput.WriteLine("exit");
                        //string output = process.StandardOutput.ReadToEnd();
                        //string output2 = process.StandardOutput.ReadLine();
                        //Console.WriteLine(output);
                        //Messagetextbox.Text = output;
                        //process.StandardOutput.Close();
                        while (!process.StandardOutput.EndOfStream)
                        {
                            string errmsg = process.StandardError.ReadLine();
                            string line = process.StandardOutput.ReadLine();
                            Console.WriteLine(errmsg+line);
                            Messagetextbox.AppendText (errmsg+line + System.Environment.NewLine);;
                            if (line.IndexOf("download completed") > -1)
                            {
                                NowDownloadbutton.Text = "开始下载";
                                NowDownloadbutton.Enabled = true;
                                MessageBox.Show("下载完成"); 
                            }
                            if (line.IndexOf("error occurred") > -1)
                            {
                                NowDownloadbutton.Text = "开始下载";
                                NowDownloadbutton.Enabled = true;
                                MessageBox.Show("下载失败"); 
                            }
                        }

                        process.StandardInput.WriteLine("exit");
                        process.WaitForExit();
                        process.Close();
                    }
                });
                newtask.Start();
            
                //Application.DoEvents();
                    
                
                
                
            }

        private void Form1_Load(object sender, EventArgs e)
        {
            Control.CheckForIllegalCrossThreadCalls = false;
            

        }

        private void Messagetextbox_TextChanged(object sender, EventArgs e)
        {
            Messagetextbox.SelectionStart = Messagetextbox.TextLength;

            // Scrolls the contents of the control to the current caret position.
            Messagetextbox.ScrollToCaret(); //Caret意思：脱字符号；插入符号; (^)
        }

        private void DownloadDirtextbox_Enter(object sender, EventArgs e)
        {
            if (DownloadDirtextbox.Focused==true)
            {
                FolderBrowserDialog dialog = new FolderBrowserDialog();

                dialog.Description = "请选择文件路径";

                if (dialog.ShowDialog() == DialogResult.OK)

                {

                    string foldPath = dialog.SelectedPath;

                    //MessageBox.Show("已选择文件夹:" + foldPath, "选择文件夹提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    DownloadDirtextbox.Text = foldPath;
                }
            }
            

        }
    }
}
